package com.jivesoftware.spark
{
	import mx.containers.Canvas;
	import mx.controls.Button;
	import mx.core.EdgeMetrics;
	import mx.core.IContainer;
	import mx.core.IFlexDisplayObject;
	import mx.core.mx_internal;
	
	use namespace mx_internal;
	
	public class CanvasButton extends Button implements IContainer
	{
		private var canvas:Canvas;
		
		public function CanvasButton():void
		{
			super();
		}
		
		private var _childrenCreated:Boolean = false;
		
		override protected function createChildren():void
		{
			super.createChildren();
			
			canvas = new Canvas();
			canvas.verticalScrollPolicy = _verticalScrollPolicy;
			canvas.horizontalScrollPolicy = _horizontalScrollPolicy;
			canvas.mouseChildren = super.mouseChildren;
			canvas.buttonMode = super.buttonMode;
			super.addChild(canvas);
			
			canvas.initializeRepeaterArrays(this);
			
			createComponents();
			
			_childrenCreated = true;
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number)
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			canvas.setActualSize(unscaledWidth, unscaledHeight);
		}
		
		override protected function measure():void
		{
			super.measure();
			
			measuredHeight = canvas.getExplicitOrMeasuredHeight();
			measuredWidth = canvas.getExplicitOrMeasuredWidth();
		}
		
		override mx_internal function layoutContents(unscaledWidth:Number,
										unscaledHeight:Number,
										offset:Boolean):void
		{
			super.layoutContents(unscaledWidth, unscaledHeight, offset);
			
			setChildIndex(canvas, numChildren - 1);
		}
		
		private var _childDescriptors : Array;
		
		public function set childDescriptors(value:Array):void
		{
			_childDescriptors = value;
		}
		
		mx_internal function setDocumentDescriptor(desc:UIComponentDescriptor):void
		{
			if (_documentDescriptor && _documentDescriptor.properties.childDescriptors)
			{
				if (desc.properties.childDescriptors)
				{
					throw new Error("Multiple sets of visual children have been specified for this component (base component definition and derived component definition).");
				}
			}
			else
			{
				_documentDescriptor = desc;
				_documentDescriptor.document = this;
			}
			
			if (desc.properties.childDescriptors)
			{
				this.childDescriptors = desc.properties.childDescriptors;
			}
		}
		
		private function createComponents():void
		{
			for each(var desc:UIComponentDescriptor in _childDescriptors)
			{
				canvas.createComponentFromDescriptor(desc, true);
			}
		}
		
		private var _horizontalScrollPolicy:String = ScrollPolicy.AUTO;
		
		public function get horizontalScrollPolicy():String
		{
			return _horizontalScrollPolicy;
		}
		
		public function set horizontalScrollPolicy(value:String):void
		{
			_horizontalScrollPolicy = value;
			
			if (canvas)
				canvas.horizontalScrollPolicy = value;
		}
		
		private var _verticalScrollPolicy:String = ScrollPolicy.AUTO;
		
		public function get verticalScrollPolicy():String
		{
			return _verticalScrollPolicy;
		}
		
		public function set verticalScrollPolicy(value:String):void
		{
			_verticalScrollPolicy = value;
			
			if (canvas)
			{
				canvas.verticalScrollPolicy = value;
			}
		}
		
		public override function get buttonMode():Boolean
		{
			return super.buttonMode;
		}
		
		public override function set buttonMode(value:Boolean):void
		{
			super.buttonMode = value;
			if (canvas)
			{
				canvas.buttonMode = value;
			}
		}
		
		public override function get mouseChildren():Boolean
		{
			return super.mouseChildren;
		}
		
		public override function set mouseChildren(enable:Boolean):void
		{
			super.mouseChildren = enable;
			if (canvas)
				canvas.mouseChildren = enable;
		}
		
		protected var _creatingContentPane:Boolean;
		public function get creatingContentPane():Boolean
		{
			return this._creatingContentPane;
		}
		public function set creatingContentPane(value:Boolean):void
		{
			this._creatingContentPane = value;
		}
		protected var _defaultButton:IFlexDisplayObject;
		public function get defaultButton():IFlexDisplayObject
		{
			return this._defaultButton;
		}
		public function set defaultButton(value:IFlexDisplayObject):void
		{
			this._defaultButton = value;
		}
		protected var _horizontalScrollPosition:Number;
		public function get horizontalScrollPosition():Number
		{
			return this._horizontalScrollPosition;
		}
		public function set horizontalScrollPosition(value:Number):void
		{
			this._horizontalScrollPosition = value;
		}
		protected var _verticalScrollPosition:Number;
		public function get verticalScrollPosition():Number
		{
			return this._verticalScrollPosition;
		}
		public function set verticalScrollPosition(value:Number):void
		{
			this._verticalScrollPosition = value;
		}
		protected var _viewMetrics:EdgeMetrics;
		public function get viewMetrics():EdgeMetrics
		{
			return this._viewMetrics;
		}
		public function set viewMetrics(value:EdgeMetrics):void
		{
			this._viewMetrics = value;
		}
	}
}
